from .model.models import Role
from app_db import db
from config import config


# 初始化 role 为 admin 和 user
def init():
    # admin
    admin_role = Role(name=config.ADMIN_ROLE, default=False, is_valid=1)
    db.session.add(admin_role)

    # user
    default_role = Role(name=config.DEFAULT_ROLE, default=True, is_valid=1)
    db.session.add(default_role)
    db.session.commit()


# 判断 角色 admin是否已存在，存在返回False
def get_admin():
    table_info = Role.query.filter_by(name=config.ADMIN_ROLE).first()
    return table_info.id if table_info else None


# 获取默认角色
def get_default_role():
    table_info = Role.query.filter_by(default=True).first()
    return table_info.id if table_info else None
